﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.DAL;


namespace QuanLyBanHang
{
    public partial class Form_SP : Form
    {
        public bool them = false;
        int masp;
        SanPham_DAL sp_dal = new SanPham_DAL();
        public Form_SP()
        {
            InitializeComponent();           
        }

        private void Form_SP_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = sp_dal.SelectSP("");
            dataGridView1.ReadOnly = true;
            dataGridView1.Columns[0].HeaderText = "Mã sản phẩm";
            dataGridView1.Columns[1].HeaderText = "Tên sản phẩm";
            dataGridView1.Columns[2].HeaderText = "Số lượng";
            dataGridView1.Columns[3].HeaderText = "Giá";
            dataGridView1.Columns[4].HeaderText = "Loại sản phẩm";
            dataGridView1.Columns[5].HeaderText = "Mô tả";
        }
        //

        private void button_tk_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = sp_dal.SelectSP(textBox_tk.Text);
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            them = true;
            button_add.Enabled = false;
            button_edit.Enabled = false;
            button_cancel.Enabled = true;
            button_del.Enabled = false;
            button_save.Enabled = true;
            dataGridView1.ReadOnly = false;
            dataGridView1.Columns[0].ReadOnly = true;
        }

        private void button_edit_Click(object sender, EventArgs e)
        {
            button_add.Enabled = false;
            button_edit.Enabled = false;
            button_cancel.Enabled = true;
            button_del.Enabled = false;
            button_save.Enabled = true;
            dataGridView1.ReadOnly = false;
            dataGridView1.Columns[0].ReadOnly = true;
            masp = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            if (them)
            {
                int n = dataGridView1.NewRowIndex - 1;
                string ten = dataGridView1.Rows[n].Cells[1].Value.ToString();
                int sl = int.Parse(dataGridView1.Rows[n].Cells[2].Value.ToString());
                int gia = int.Parse(dataGridView1.Rows[n].Cells[3].Value.ToString());
                string loai = dataGridView1.Rows[n].Cells[4].Value.ToString();
                string mota = dataGridView1.Rows[n].Cells[5].Value.ToString();
                if (sp_dal.insertSP(ten, sl, gia, loai, mota)) MessageBox.Show("Thành công");
                else MessageBox.Show("Có lỗi");

            }
            else
            {
                int n = dataGridView1.CurrentRow.Index ;
                //MessageBox.Show(n.ToString());
                string ten = dataGridView1.Rows[n].Cells[1].Value.ToString();
                int sl = int.Parse(dataGridView1.Rows[n].Cells[2].Value.ToString());
                int gia = int.Parse(dataGridView1.Rows[n].Cells[3].Value.ToString());
                string loai = dataGridView1.Rows[n].Cells[4].Value.ToString();
                string mota = dataGridView1.Rows[n].Cells[5].Value.ToString();
                if (sp_dal.updateSP(masp, ten, sl, gia, loai, mota)) MessageBox.Show("Thành công");
                else MessageBox.Show("Có lỗi");
            }
            them = false;
            button_add.Enabled = true;
            button_edit.Enabled = true;
            button_cancel.Enabled = false ;
            button_del.Enabled = true;
            button_save.Enabled = false;
            dataGridView1.ReadOnly = true;
            dataGridView1.DataSource = sp_dal.SelectSP(textBox_tk.Text);
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            them = false;
            button_add.Enabled = true;
            button_edit.Enabled = true;
            button_cancel.Enabled = false;
            button_del.Enabled = true;
            button_save.Enabled = false;
            dataGridView1.ReadOnly = true;
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_del_Click(object sender, EventArgs e)
        {
            int id = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            if (sp_dal.deleteSP(id)) MessageBox.Show("Đã xóa");
            else MessageBox.Show("Có lỗi");
            dataGridView1.DataSource = sp_dal.SelectSP(textBox_tk.Text);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        
    }
}
